home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Educational / HodgePodge3 / Source / Krinkle.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.2 KB  |  136 lines

  1. // Krinkle.m --
  2.  
  3. #import "math.h"
  4. #import <appkit/NXBitmapImageRep.h>
  5. #import "Krinkle.h"
  6.  
  7. @implementation Krinkle
  8.  
  9. long random(),
  10. force();
  11.  
  12. #define YMAX 200
  13. #define XMAX 320
  14. #define YMAX1 50
  15. #define XMAX1 50
  16. #define SCALE 256
  17. #define LSCALE 8
  18. #define SCmax 32767
  19. #define SCmin -32768
  20.  
  21. typedef char char_image[YMAX1][XMAX1];
  22. Krinkle *Self;
  23.  
  24. - init
  25. {    [super init];
  26. #    define FC(x) ((int)((x)*SCALE))        /* all f24.8*/
  27.                     // user interface:
  28.     krinkle = FC(15.);
  29.     bump = FC(8.);
  30.     spring = FC(2.);
  31.     damping = FC(.1);
  32.     //damping = 0.1;
  33.  
  34.     agc = 0x10000;
  35.     agv = 0;
  36.  
  37.     Xf = &z1;    Xt = &z2;
  38.     Vf = &v1;    Vt = &v2;
  39.     return [self Randomize :nil];
  40. }
  41.  
  42. - Randomize :sender;
  43. {    int i, j;
  44.     for (i=0; i<c_yh; i++)
  45.         for (j=0; j<c_xw; j++)
  46.             z1[i][j] = random() % (10);        //?? n?
  47.     return self;
  48. }
  49.  
  50. - (void)compute
  51. {    register int Iold;
  52.     register long f, v, p;
  53.     register int_image *pXf, *pVf;
  54.     int pmax, pmin;
  55.     int_image *pXt, *pVt;
  56.     int x, y;
  57.     int_image *tmp;
  58.     screen_image *pS;
  59.  
  60.     Self = self;
  61.     pmax = -0xffff; pmin = 0xffff;
  62.     for (y=1; y<c_yh-1; y++)
  63.     {    pXf = (int_image *)&(*Xf)[y][1];
  64.         pXt = (int_image *)&(*Xt)[y][1];
  65.         pVf = (int_image *)&(*Vf)[y][1];
  66.         pVt = (int_image *)&(*Vt)[y][1];
  67.         pS = (screen_image *)&image[y][1];
  68.         for (x=1; x<c_xw-1; x++)
  69.         {    Iold = (*pXf)[0][0];
  70.             f = (force((*pXf)[-1][0],Iold,(*pXf)[1][0])
  71.                 + force((*pXf)[0][-1],Iold,(*pXf)[0][1])
  72.                 - (long)damping * (*pVf)[0][0] ) >>LSCALE;
  73.             (*pVt)[0][0] = v = (*pVf)[0][0] + f;
  74.             (*pXt)[0][0] = p=(*pXf)[0][0] + (v>>LSCALE);/**/
  75.         /*    (*pXt)[0][0] = p=((*pXf)[0][0] + v)>>LSCALE;/**/
  76.     
  77.             if (p > SCmax) p = SCmax;
  78.             if (p < SCmin) p = SCmin;
  79.             if (p > pmax) pmax = p;
  80.             if (p < pmin) pmin = p;
  81.     //        (*pS)[0][0][0] = ((Iold-agv) * agc) >>8;
  82.     ADD BACK!!!
  83.         
  84.             pXt = (int_image *)&(*pXt)[0][1];
  85.             pXf = (int_image *)&(*pXf)[0][1];
  86.             pVt = (int_image *)&(*pVt)[0][1];
  87.             pVf = (int_image *)&(*pVf)[0][1];
  88.             pS  = (screen_image *)&(*pS) [0][1];
  89.     }    }
  90.     agc = 0x10000 / (pmax-pmin+1);
  91.     agv = pmin;
  92.     tmp = Xf; Xf = Xt; Xt = tmp;
  93.     tmp = Vf; Vf = Vt; Vt = tmp;
  94. }
  95.  
  96. #define randm(x) (( ((int)random()) % (2*x-1) )-x)
  97. #define bumpy(x) (x < -SCALE)? 0:\
  98.          (x<0)? -SCALE-x:\
  99.          (x==0)? randm(SCALE):\
  100.          (x< SCALE)? SCALE-d:\
  101.          0
  102.  
  103. long
  104. force(a,b,c)
  105. register int a, /* lesser index point */
  106.          b, /* this index point */
  107.          c; /* next index point */
  108.         /* all i12.4 */
  109. {    register int d, e;
  110.     d = b - a;    /* this is the bump that causes all the problems: */
  111.     e = c - b;
  112.     d = bumpy(d);            /*__ |\__*/
  113.     e = bumpy(e);            /*  \|   */
  114.  
  115.     return  ( Self->krinkle * (a-2*b+c)        /* 2'nd derivative */
  116.         + Self->bump * (d - e)             /* 1'st derivative */
  117.         - Self->spring * b );            /* position */
  118. }    
  119.  
  120.  
  121. - drawSelf:(const NXRect *)rects :(int)rectCount
  122. {    //k2 = (float)[Healthy2Ill floatValue];
  123.     if (bitmap == 0)
  124.     {    bitmap = [[NXBitmapImageRep alloc]         initData:(unsigned char *)image 
  125.                 pixelsWide :c_xw                    pixelsHigh :(int)c_yh
  126.                 bitsPerSample :8                    samplesPerPixel :Ncolors
  127.                 hasAlpha :NO                        isPlanar :NO
  128.                 colorSpace:Ncolors>1? NX_RGBColorSpace:NX_OneIsWhiteColorSpace
  129.                 bytesPerRow :0                    bitsPerPixel :0];
  130.         [self setOpaque:YES];
  131.         [self compute];
  132.         [self start :nil];
  133.     }
  134.     [bitmap drawIn :&bounds];
  135.     return self;
  136. }
  137.  
  138. @end
  139.